package com.example.demo.exception;

import org.junit.Test;

public class ExceptionTest1 {

    // 除零异常
    public static void main(String[] args) {
        System.out.println(1/0);
    }

    // 空指针异常
    @Test
    public void testException() {
        Object obj = null;
        System.out.println(obj.toString());
    }

    // 数组越界异常
    @Test
    public void testException2() {
        int[] arr = new int[5];
        System.out.println(arr[6]);
    }

    // 字符串越界异常
    @Test
    public void testException3() {
        String str = "Hello";
        System.out.println(str.charAt(10));
    }

    // 数组下标越界异常
    @Test
    public void testException4() {
        int[] arr = new int[5];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
        System.out.println(arr[6]);
    }

    // 数组下标越界异常
    @Test
    public void testException5() {
        int[] arr = new int[5];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
        System.out.println("11111111111111111111111");
        System.out.println(arr[6]);
        System.out.println("2222222222222222222222222");
        System.out.println("finish");
    }

    // 数组下标越界异常  捕获异常
    @Test
    public void testException6() {
        int[] arr = new int[5];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
        try {
            System.out.println("11111111111111111111111");
            System.out.println(arr[6]);
            System.out.println("2222222222222222222222222");
        }catch(Exception e) {
            System.out.println("发生了异常");
        }
        System.out.println("finish");
    }

    // 捕获异常， finally块还是会执行
    @Test
    public void testException7() {
        int[] arr = new int[5];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
        try {
            System.out.println("11111111111111111111111");
            System.out.println(arr[6]);
            System.out.println("2222222222222222222222222");
        }catch(Exception e) {
            System.out.println("发生了异常");
        }finally {
            System.out.println("finally");
        }
        System.out.println("finish");
    }

    // 捕获异常， finally块还是会执行
    @Test
    public void testException8() {
        System.out.println(testresult());
    }

    public static String testresult(){
        int[] arr = new int[5];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
        try {
            System.out.println("11111111111111111111111");
            System.out.println(arr[6]);
            System.out.println("2222222222222222222222222");
        }catch(Exception e) {
            System.out.println("发生了异常");
        }finally {
            System.out.println("finally");
        }
        System.out.println("finish");
        return "123";
    }

    // 捕获异常， finally块还是会执行
    @Test
    public void testException9() {
        System.out.println(testresult111());
    }


    /**
     * finally 无论是否发生异常，一定会执行，
     * @return
     */
    public static String testresult111(){
        int[] arr = new int[5];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
        try {
            System.out.println("11111111111111111111111");
            System.out.println(arr[1]);
            System.out.println("2222222222222222222222222");
            return "1231111111111111";
        }catch(Exception e) {
            System.out.println("发生了异常");
        }finally {
            System.out.println("finally");
        }
        System.out.println("finish");
        return "123222222222222222";
    }
}
